package com.mingyizhudao.app.view.doctor;

import java.util.HashMap;
import android.os.Bundle;
import android.provider.Telephony.Carriers;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.network.Net;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.doctor.bean.DoctorDetailsEntity;
import com.mingyizhudao.app.view.doctor.bean.DoctorsEntity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.handler.UMWXHandler;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

public class DoctorDetailsActivity extends MYBaseActivity{

	private ImageView iv_doctor;

	private TextView tv_name,tv_atitle,tv_dept,tv_mtitle,tv_hospital,tv_good_at,tv_career_exp;

	private LinearLayout ll_reason,ll_good_at ,ll_honour,ll_career_exp;
	private RelativeLayout rl_reason;
	private Button btn_order ;
	private DoctorDetailsEntity detailsEntity;
	private final String detailUrl = "http://m.mingyizhudao.com/mobile/doctor/view/id/";
	private DoctorsEntity doctorListEntity;
	final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
            {
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,//SHARE_MEDIA.SINA,
                SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE
            };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctor_details);
		initView();
		getDoctorDetails();
	}

	@Override
	public void initHeaderView() {
		setTitleViewValue(0,0,R.color.white);
		setTitleViewValue("医生详情");
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		initOptionView(R.drawable.share, 0, new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
                new ShareAction(activity).setDisplayList( displaylist )
                        .withText( "名医主刀" )
                        .setShareboardclickCallback(shareBoardlistener)
                        .open();
			}
		});
	}
	
	private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {

		@Override
		public void onclick(SnsPlatform arg0, SHARE_MEDIA arg1) {
			
			if(detailsEntity != null){
				UMImage image =null;
				if (detailsEntity.getImageUrl()!=null) {
					image = new UMImage(activity, detailsEntity.getImageUrl());
				}
				
				String url = detailUrl + detailsEntity.getId();
				String dsc = detailsEntity.getName()+" "+detailsEntity.getaTitle() + " "+detailsEntity.getHospitalName();
				String context = "国内顶尖外科手术专家";
				String descrption = detailsEntity.getDescription();
				if(Utils.isStringEmpty(descrption)){
					String careerExp = detailsEntity.getCareerExp();
					if(!Utils.isStringEmpty(careerExp)){
						context = careerExp;
					}
				}else{
					context = descrption;	
				}
				
				new ShareAction(activity).setPlatform(arg1).setCallback(umShareListener)
		          .withText(context)
		          .withTitle(dsc)
		          .withMedia(image)
		          .withTargetUrl(url)
		          .share();
			}
			
//			if(arg1 == SHARE_MEDIA.WEIXIN){
//				new ShareAction(activity).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
//		          .withText(context)
//		          .withTitle(dsc)
//		          .withMedia(image)
//		          .withTargetUrl(url)
//		          .share();
//			}else if(arg1 == SHARE_MEDIA.SINA){
//				new ShareAction(activity).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
//		          .withText(context)
//		          .withTitle(dsc)
//		          .withMedia(image)
//		          .withTargetUrl(url)
//		          .share();
//			}else if(arg1 == SHARE_MEDIA.WEIXIN_CIRCLE){
//				new ShareAction(activity).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
//		          .withText(context)
//		          .withTitle(dsc)
//		          .withMedia(image)
//		          .withTargetUrl(url)
//		          .share();
//			}else if(arg1 == SHARE_MEDIA.QQ){
//				new ShareAction(activity).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
//		          .withText(context)
//		          .withMedia(image)
//		          .withTitle(dsc)
//		          .withTargetUrl(url)
//		          .share();
//			}else if(arg1 == SHARE_MEDIA.QZONE){
//				new ShareAction(activity).setPlatform(SHARE_MEDIA.QZONE).setCallback(umShareListener)
//		          .withText(context)
//		          .withTitle(dsc)
//		          .withMedia(image)
//		          .withTargetUrl(url)
//		          .share();
//			}
		}
    };
	
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(activity, platform + " 发送成功！", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(activity,platform + " 发送失败！", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(activity,platform + " 发送取消！", Toast.LENGTH_SHORT).show();
        }
    };


	private void initView() {

		doctorListEntity = (DoctorsEntity) getIntent().getSerializableExtra("doctorListEntity");

		iv_doctor = (ImageView)findViewById(R.id.iv_doctor);
		tv_name = (TextView)findViewById(R.id.tv_name);
		tv_atitle = (TextView)findViewById(R.id.tv_atitle);
		tv_dept = (TextView)findViewById(R.id.tv_dept);
		tv_mtitle = (TextView)findViewById(R.id.tv_mtitle);
		tv_hospital = (TextView)findViewById(R.id.tv_hospital);
		tv_good_at = (TextView)findViewById(R.id.tv_good_at);
		tv_career_exp = (TextView)findViewById(R.id.tv_career_exp);

		ll_reason = (LinearLayout)findViewById(R.id.ll_reason);
		ll_good_at = (LinearLayout)findViewById(R.id.ll_good_at);
		ll_honour = (LinearLayout)findViewById(R.id.ll_honour);
		ll_career_exp = (LinearLayout)findViewById(R.id.ll_career_exp);
		rl_reason = (RelativeLayout)findViewById(R.id.rl_reason);

		rl_reason.setVisibility(View.GONE);
		ll_good_at.setVisibility(View.GONE);
		ll_honour.setVisibility(View.GONE);
		ll_career_exp.setVisibility(View.GONE);

		btn_order = (Button)findViewById(R.id.btn_order);

		if( "1".equals( doctorListEntity.getIsContracted() ) )
		{
			btn_order.setVisibility( View.VISIBLE );
			btn_order.setOnClickListener( new OnClickListener()
			{
				@Override
				public void onClick( View v )
				{
					CommonUtils.goToCreateOrder(activity, doctorListEntity.getId(), doctorListEntity.getName(), doctorListEntity.getHpDeptName(), doctorListEntity.getHpName(), doctorListEntity.getActionUrl(),false);
				}
			} );

		}else {
			btn_order.setVisibility( View.GONE );
		}

		iv_doctor.setImageResource(R.drawable.default_doctor);
		String tempUrl = doctorListEntity.getImageUrl();
		if (tempUrl != null && tempUrl.length() > 0) {
			Net.displayImage(tempUrl, iv_doctor,R.drawable.default_doctor);
		}

		tv_name.setText(doctorListEntity.getName());

		String aTitle=  doctorListEntity.getaTitle();
		if (aTitle!=null&&(!"null".equals(aTitle))) {
			tv_atitle.setText(aTitle);
		}
		tv_dept.setText(doctorListEntity.getHpDeptName());

		String mTitle=  doctorListEntity.getmTitle();
		if (mTitle!=null&&(!"null".equals(mTitle))) {
			tv_mtitle.setText(mTitle);
		}

		tv_hospital.setText(doctorListEntity.getHpName());

	}

	private void getDoctorDetails() {
		String actionUrl = CommonUtils.getDoctorUrl(activity)+ doctorListEntity.getId() ;
		LoadingDialog.getInstance(activity).show();
		QJNetUICallback callback = new QJNetUICallback<QjResult<HashMap<String, DoctorDetailsEntity>>>(
				activity) {
			@Override
			public void onSuccess(QjResult<HashMap<String, DoctorDetailsEntity>> result) {

				if(result != null){
					setDoctorDetails(result.getResults().get("doctor"));
				}
			}

			public void onError(Exception e, QjResult<HashMap<String, DoctorDetailsEntity>> result) {
				super.onError(e, result);
			};

			public void onCompleted(Exception e, QjResult<HashMap<String, DoctorDetailsEntity>> result) {
				super.onCompleted(e, result);
			};
		};
		String path = new DataHandler().getDataObject(activity, actionUrl,
				null, new TypeToken<QjResult<HashMap<String, DoctorDetailsEntity>>>() {
		}, callback);

	}

	private void setDoctorDetails(DoctorDetailsEntity doctorDetailsEntity) {
		detailsEntity = doctorDetailsEntity;
		iv_doctor.setImageResource(R.drawable.default_doctor);
		String tempUrl = doctorDetailsEntity.getImageUrl();
		if (tempUrl != null && tempUrl.length() > 0) {
			Net.displayImage(tempUrl, iv_doctor,R.drawable.default_doctor);
		}

		tv_name.setText(doctorDetailsEntity.getName());

		String aTitle=  doctorDetailsEntity.getaTitle();
		if (aTitle!=null&&(!"null".equals(aTitle))) {
			tv_atitle.setText(aTitle);
		}
		tv_dept.setText(doctorDetailsEntity.getHpDeptName());

		String mTitle=  doctorDetailsEntity.getmTitle();
		if (mTitle!=null&&(!"null".equals(mTitle))) {
			tv_mtitle.setText(mTitle);
		}

		tv_hospital.setText(doctorDetailsEntity.getHospitalName());


		String description=  doctorDetailsEntity.getDescription();
		if (description!=null&&(!"null".equals(description))) {
			ll_good_at.setVisibility(View.VISIBLE);
			tv_good_at.setText(description);
		}else {
			ll_good_at.setVisibility(View.GONE);
		}

		String vareerExp=  doctorDetailsEntity.getCareerExp();
		if (vareerExp!=null&&(!"null".equals(vareerExp))) {
			ll_career_exp.setVisibility(View.VISIBLE);
			tv_career_exp.setText(vareerExp);
		}else {
			ll_career_exp.setVisibility(View.GONE);
		}

		String honour[] = doctorDetailsEntity.getHonour();
		if (honour != null && honour.length> 0) {
			ll_honour.setVisibility(View.VISIBLE);

			for (int i = 0; i < honour.length; i++) {

				View contentView = getLayoutInflater().inflate(R.layout.view_num, null);

				ImageView iv_num = (ImageView)contentView.findViewById(R.id.iv_num);
				TextView tv_num = (TextView)contentView.findViewById(R.id.tv_num);
				iv_num.setImageResource(R.drawable.doctor_details_honor_number);
				tv_num.setText(honour[i]);
				ll_honour.addView(contentView);
			}

		} else {
			ll_honour.setVisibility(View.GONE);
		} 


		String reasons[] = doctorDetailsEntity.getReasons();
		if (reasons != null && reasons.length> 0) {
			rl_reason.setVisibility(View.VISIBLE);

			for (int i = 0; i < reasons.length; i++) {

				View contentView = getLayoutInflater().inflate(R.layout.view_num, null);

				ImageView iv_num = (ImageView)contentView.findViewById(R.id.iv_num);
				//iv_num.setScaleType(ScaleType.CENTER);
				TextView tv_num = (TextView)contentView.findViewById(R.id.tv_num);
				iv_num.setImageResource(R.drawable.doctor_details_honor_number);
				tv_num.setText(reasons[i]);
				ll_reason.addView(contentView);
			}

		} else {
			rl_reason.setVisibility(View.GONE);
		} 

	}
	
}
